﻿using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.Common;
using RentHouse.DTO;
using RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RentHouse.Admin.Controllers
{
    public class HouseController : Controller
    {
        private readonly IHouseService _houseService;
        private readonly IRegionService _regionService;
        private readonly IIdNameService _idNameService;
        private readonly ICommunityService _communityService;

        public HouseController(IHouseService houseService,IRegionService regionService,IIdNameService idNameService,ICommunityService communityService)
        {
            _houseService = houseService;
            _regionService = regionService;
            _idNameService = idNameService;
            _communityService = communityService;
        }
        // GET: House
        /// <summary>
        /// 房源列表
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        
        public ActionResult Index(int typeId)
        {
            ViewBag.typeId = typeId;
            return View();
        }

        [HttpPost]
        public ActionResult GetHouseList(string communityName,int typeId,int start,int length)
        {
            int housecount = 0;
            var list = _houseService.GetHouseList(communityName,typeId,start,length, ref housecount);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsFiltered = housecount;
            pageModel.recordsTotal = housecount;
            return Json(pageModel);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
           
            //获取当前登录管理员的信息，根据当前管理员所管理的地区，来获取对应的区域
            var currentAdmin = User as MyFormsPrincipal<LoginAdminDTO>;
            //根据CityId获取当前所在的区域
            ViewBag.RegionList = _regionService.GetRegionList(currentAdmin.UserData.CityId).Select(e => new SelectListItem()
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //房型
            ViewBag.RoomTypeList = _idNameService.GetIdNameList(IdNameEnum.户型).Select(e => new SelectListItem()
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //装修类型
            ViewBag.DecorateStatusList = _idNameService.GetIdNameList(IdNameEnum.装修状态).Select(e => new SelectListItem()
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //状态
            ViewBag.StatusList = _idNameService.GetIdNameList(IdNameEnum.房屋状态).Select(e => new SelectListItem()
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //类型
            ViewBag.TypeList = _idNameService.GetIdNameList(IdNameEnum.房屋类型).Select(e => new SelectListItem()
            {
                Text = e.Name,
                Value = e.Id.ToString()
            });
            //房源设施
            var AttachmentList = _houseService.GetAttachmentList();
            return View(AttachmentList);
        }

        //提交添加房源表单
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(HouseAddDTO dto)
        {
            AjaxResult result = _houseService.AddHouse(dto);
            return Json(result);
        }
    }
}